<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{common/header :: commonHead}"></head>
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>秒杀商品列表</h5>
                    <div class="ibox-tools">
                        <a href="/shopping_kill/add" title="添加"><i class="glyphicon glyphicon-plus-sign"></i></a>

                        <a href="javascript: window.location.href='/shopping_kill/list';" title="刷新"><i class="glyphicon glyphicon-repeat"></i></a>

                    </div>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-3" style="float: right">
                            <div class="input-group">
                                <input type="text" placeholder="请输入关键词" id="search-text" class="input-sm form-control">
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-sm btn-primary" id="search-btn"> 搜索</button>
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12">
                            <div id="report_table"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script src="/layui/layui.js"></script>
<script th:inline="javascript">
    let keyword = '';
    let page = 1;
    let size = 10;
    loadTable();
    function loadTable() {
        layui.use('table', function(){
            table = layui.table;
            table.render({
                id: 'report_table',
                elem: '#report_table',
                height: 600,
                url: ipDomain + '/quanquan-store/shopping_kill/query',
                size: 'sm',
                skin: 'line',
                method: 'post',
                limits:[10,20,40,100],
                request: {
                    pageName: 'page',
                    limitName: 'size'
                },
                response: {
                    statusName: 'status', //规定数据状态的字段名称，默认：code
                    statusCode: 200, //规定成功的状态码，默认：0
                    msgName: 'msg', //规定状态信息的字段名称，默认：msg
                    countName: 'total', //规定数据总数的字段名称，默认：count
                    dataName: 'rows' //规定数据列表的字段名称，默认：data
                },
                page: true,
                cols: [[ //表头
                    {field: 'shoppingKillName', title: '秒杀商品名称',fixed: 'left',sort:true},
                    {field: 'startTime', title: '秒杀开始时间', sort: true, fixed: 'left'},
                    {field: 'endTime', title: '秒杀结束时间', sort: true, fixed:'left'},
                    {field: 'inventory', title: '商品原始库存'},
                    {field: 'remainingInventory', title: '商品剩余库存'},
                    {field: 'price', title: '秒杀价格（现金）'},
                    {field: 'integral', title: '秒杀价格（积分）',width: 70},
                    {field: 'killStatus', title: '秒杀商品状态',templet :function (e) {
                        return  e.killStatus == 'preparing'?'准备中':(e.killStatus == 'processing'?'进行中':'已结束');
                        }},
                    {field: 'createTime', title: '创建时间', sort: true},
                    {title: '操作', templet :function (e) {
                        var id = e.id;
                        var status_ = e.show;
                        var class_ = !status_ ? 'btn btn-xs btn-danger' : 'btn btn-xs btn-primary';
                        var up_down = !status_ ? '下架' : '上架';
                        var temp =
                            !status_ ?
                                '<button class="btn btn-xs btn-primary" onclick="edit(\''  + id + '\')" disabled>编辑</button> '
                                + '<button onclick="changeStatus($(this),\'' +id+'\')" class="' + class_ + '">' + up_down + '</button>'
                            :
                                '<button class="btn btn-xs btn-primary" onclick="edit(\''  + id + '\')">编辑</button> '
                                + '<button onclick="changeStatus($(this),\'' +id+'\')" class="' + class_ + '">' + up_down + '</button>'
                                + ' <button onclick="changeInventory($(this),\'' +id+'\')" class="' + class_ + '">修改库存</button>'
                        ;
                        return temp;
                        }
                    }
                ]],
                done: function(res, curr, count){
                    page = curr;
                    size = res.size;
                }
            });
        });
    }

    function changeInventory(e,id) {
        var editorBtn = e.prev();
        window.location.href = '/shopping_kill/edit_inventory?id='+id;
    }



    function changeStatus(e,id) {
        var editorBtn = e.prev();
        $.ajax({
            url : ipDomain + '/quanquan-store/shopping_kill/put_away',
            type : 'POST',
            data : {id:id},
            success : function (res) {
                var show_ = res.data.show;
                if (show_) {
                    e.html('上架');
                    editorBtn.removeAttr('disabled');
                    e.removeClass();
                    e.addClass('btn btn-xs btn-primary');
                } else {
                    e.html('下架');
                    editorBtn.attr('disabled',true);
                    e.removeClass();
                    e.addClass('btn btn-xs btn-danger')
                }
            }
        });
    }

    function edit(id) {
        window.location.href = '/shopping_kill/edit?id='+id;
    }

    $("#search-btn").click(function(){
        keyword = $("#search-text").val();
        reloadTable(page,size);
    });
    function reloadTable(page,size) {
        table.reload(
            'report_table',
            {
                where: {
                    'keyword':keyword,
                    'page':page,
                    'size':size
                }
            }
        );
    }
</script>
</html>